package com.jinlejia.bbs.adapter.mine;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.BaseLoadAdapter;
import com.jinlejia.bbs.bean.mine.AttentionBean;
import com.jinlejia.bbs.databinding.BbsAdapterFansItemBinding;
import com.jinlejia.bbs.logic.TakeAttentionLogic;
import com.jinlejia.bbs.ui.mine.others.BBSOtherUserMainActivity;

import java.util.ArrayList;

/**
 * @author 王进 on 2019/3/13.
 */

public class AttentionAdapter extends BaseLoadAdapter<ArrayList<AttentionBean>> {


    private final int DATA_TYPE = 2;

    public AttentionAdapter(Context context) {
        super(context);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case DATA_TYPE:
                viewHolder = new AttentionViewHolder(inflater.inflate(R.layout.bbs_adapter_fans_item, parent, false));
                break;
            default:
                throw new IllegalArgumentException("未知类型");
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AttentionViewHolder){
            bindFansViewHolder((AttentionViewHolder) holder,position);
        }
    }

    //endregion

    //region 粉丝列表
    class AttentionViewHolder extends RecyclerView.ViewHolder {
        AttentionViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
    private void bindFansViewHolder(AttentionViewHolder holder, final int position) {
        final AttentionBean bean = mData.get(position);
        BbsAdapterFansItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        binding.setBean(bean);
        int state = bean.getAttentionState();
        if (state==0){
            binding.attentionStateTextView.setText("关注");
            binding.attentionStateTextView.setTextColor(Color.parseColor("#FCCB00"));
            binding.attentionStateTextView.setBackgroundResource(R.drawable.bbs_shape_button_checked);
        }else if (state==1){
            binding.attentionStateTextView.setTextColor(Color.parseColor("#999999"));
            binding.attentionStateTextView.setBackgroundResource(R.drawable.bbs_shape_button_normal);
            binding.attentionStateTextView.setText("已关注");
        }else if (state==2){
            binding.attentionStateTextView.setTextColor(Color.parseColor("#999999"));
            binding.attentionStateTextView.setBackgroundResource(R.drawable.bbs_shape_button_normal);
            binding.attentionStateTextView.setText("互相关注");
        }

        binding.attentionStateTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TakeAttentionLogic takeAttentionLogic = new TakeAttentionLogic(mContext);
                takeAttentionLogic.execute(bean.getUser().getUserPin(), new TakeAttentionLogic.LogicCallBack() {
                    @Override
                    public void onResult(int result) {
                        switch (result){
                            case 0: //未关注，提示用户进行关注
                                bean.setAttentionState(0);
                                break;
                            case 1:
                                bean.setAttentionState(1);
                                break;
                            case 2:
                                bean.setAttentionState(2);
                                break;
                        }
                        notifyItemChanged(position);

                    }
                });
            }
        });
        binding.roundImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, BBSOtherUserMainActivity.class);
                String pin = bean.getUser().getUserPin();
                intent.putExtra("pin",pin);
                mContext.startActivity(intent);
            }
        });
    }
    //endregion


    @Override
    protected int getItemViewTypeZ(int position) {
        return DATA_TYPE;
    }

    @Override
    public int getItemCount() {
        if (mData==null || mData.size()==0){
            return 1;
        }
        return mData.size();
    }
}
